package com.hk.Mgr.web.controller.app.yingmi;

import com.hk.api.ProcessAPI;
import com.hk.app.utils.TimeService;
import com.hk.app.utils.YingmiOpenAPI;
import com.hk.base.BaseCtrl;
import com.hk.commons.entity.sys.SysUser;
import com.hk.commons.utils.sys.MapHelper;
import com.hk.commons.vo.JsonResult;
import com.hk.commons.vo.yingmi.GetWalletOrdersListReq;
import com.hk.commons.vo.yingmi.RechargeWalletReq;
import com.hk.commons.vo.yingmi.RedeemWalletReq;
import com.hk.yingmi.YingmiOpenAPIConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lipei on 2017/8/25.
 * 盈米宝相关的类
 *
 * 01-盈米宝充值接口
 * 02-盈米宝取现
 * 03-查询账户盈米宝持有 ,可用于盈米宝的货币基金列表
 * 04-获取用户盈米宝余额
 * 05-查询账户所有支付方式和盈米宝余额接口
 * 06-获取盈米宝订单接口
 */
@Controller
@RequestMapping(value = BaseCtrl.App + "/yingmiAPI/wallet")
public class WalletCtrl {


    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private YingmiOpenAPI openAPI;

    /**
     * 01-盈米宝充值接口
     * @param request
     * @param rechargeWalletReq
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/rechargeWallet", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public JsonResult rechargeWallet(HttpServletRequest request, @RequestBody RechargeWalletReq rechargeWalletReq) {

        ProcessAPI api;
        api = (jsonResult, validate) -> {
            //检查必传
            validate.check("brokerUserId", rechargeWalletReq.getBrokerUserId());
            validate.check("accountId", rechargeWalletReq.getAccountId());
            validate.check("paymentMethodId", rechargeWalletReq.getPaymentMethodId());
            validate.check("fundCode", rechargeWalletReq.getFundCode());
            validate.check("brokerOrderNo", rechargeWalletReq.getBrokerOrderNo());

            Map<String, String> parMap = MapHelper.beanToMap(rechargeWalletReq);
            logger.info("parm Map = {}", parMap);
            String result = openAPI.sslPost(YingmiOpenAPIConstant.TRADE_RECHAEGE_WALLET, parMap);
            logger.info("result = {}", result);
            openAPI.processJsonResult(jsonResult,result);
        };

        return api.process(request);
    }

    /**
     *  02-盈米宝取现
     * @param request
     * @param redeemWalletReq
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/redeemWallet", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public JsonResult redeemWallet(HttpServletRequest request, @RequestBody RedeemWalletReq redeemWalletReq) {

        ProcessAPI api = (jsonResult, validate) -> {

            validate.check("brokerUserId", redeemWalletReq.getBrokerUserId());
            validate.check("accountId", redeemWalletReq.getAccountId());
            validate.check("walletId", redeemWalletReq.getWalletId());
            validate.check("tradeShare",redeemWalletReq.getTradeShare());
            validate.check("brokerOrderNo",redeemWalletReq.getBrokerOrderNo());

            Map<String, String> parMap = MapHelper.beanToMap(redeemWalletReq);
            logger.info("parm Map = {}", parMap);
            String result = openAPI.sslPost(YingmiOpenAPIConstant.TRADE_REDEEMWALLET, parMap);
            logger.info("result = {}", result);
            openAPI.processJsonResult(jsonResult,result);

        };

        return api.process(request);
    }


    /**
     *  03-查询账户盈米宝持有 ,可用于盈米宝的货币基金列表
     * @param request
     * @return
     */
    @RequestMapping(value = YingmiOpenAPIConstant.YINGMI_API_ACCOUNT_GET_WALLET_FUNDS, method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getWalletFunds(HttpServletRequest request, Long userId, String accountId) {
        ProcessAPI api = (jsonResult, validate) -> {
            SysUser user = openAPI.getValidatedUser(userId, validate);

            Map<String, String> params = new HashMap<>();
            params.put("brokerUserId", user.getName());
            params.put("accountId", accountId);

            String jsonResp = openAPI.sslGet_ByMap(YingmiOpenAPIConstant.TRADE_GET_WALLET_FUNDS, params, null);

            openAPI.processJsonResult(jsonResult, jsonResp);
        };

        return api.process(request);
    }

    /**
     * 04-获取用户盈米宝余额
     * @param request
     * @param brokerUserId
     * @param accountId
     * @return
     */
    @RequestMapping(value = "/getWalletShares", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getWalletShares(HttpServletRequest request, String brokerUserId, String accountId) {
        ProcessAPI api = (jsonResult, validate) -> {

            validate.check("brokerUserId",  brokerUserId);
            validate.check("accountId", accountId);

            Map<String, String> params = new HashMap<>();
            params.put("brokerUserId", brokerUserId);
            params.put("accountId", accountId);

            String jsonResp = openAPI.sslGet_ByMap(YingmiOpenAPIConstant.TRADE_GET_WALLET_SHARES, params, null);

            openAPI.processJsonResult(jsonResult, jsonResp);
        };

        return api.process(request);
    }

    /**
     * 05-查询账户所有支付方式和盈米宝余额接口
     * @param request
     * @param brokerUserId 投资者在合作平台的用户ID
     * @param accountId 投资者盈米财富账户ID
     * @return
     */
    @RequestMapping(value = "/getPaymentMethodsAndWalletShares", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getPaymentMethodsAndWalletShares(HttpServletRequest request, String brokerUserId, String accountId) {
        ProcessAPI api = (jsonResult, validate) -> {

            validate.check("brokerUserId",  brokerUserId);
            validate.check("accountId", accountId);

            Map<String, String> params = new HashMap<String, String>();
            params.put("brokerUserId", brokerUserId);
            params.put("accountId", accountId);

            logger.info("parm Map = {}", params);
            String jsonResp = openAPI.sslGet_ByMap(YingmiOpenAPIConstant.TRADE_GETPAYMENTMETHODSANDWALLETSHARES, params, null);
            logger.info("parm Map = {}", jsonResp);
            openAPI.processJsonResult(jsonResult, jsonResp);
        };

        return api.process(request);
    }

    /**
     * 06-获取盈米宝订单接口
     * @param request
     * @param getWalletOrdersListReq
     * @return
     */
    @RequestMapping(value = "/getWalletOrders", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult getWalletOrders(HttpServletRequest request, @RequestBody GetWalletOrdersListReq getWalletOrdersListReq) {
        ProcessAPI api = (jsonResult, validate) -> {

            validate.check("brokerUserId",  getWalletOrdersListReq.getBrokerUserId());
            validate.check("accountId", getWalletOrdersListReq.getAccountId());
            validate.check("page", getWalletOrdersListReq.getPage());
            validate.check("size", getWalletOrdersListReq.getSize());

            Map<String, String> parMap = MapHelper.beanToMap(getWalletOrdersListReq);
            logger.info("parm Map = {}", parMap);
            String result = openAPI.sslGet_ByMap(YingmiOpenAPIConstant.TRADE_GETWALLETORDERS, parMap, null);
            logger.info("result = {}", result);
            openAPI.processJsonResult(jsonResult,result);

        };

        return api.process(request);
    }

    /**
     *  07-盈米宝快速取现
     * @param request
     * @param redeemWalletReq
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/quickRedeemWalletPossibleDate", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public JsonResult redeemWalletDate(HttpServletRequest request,RedeemWalletReq redeemWalletReq) {

        ProcessAPI api = (jsonResult, validate) -> {

            jsonResult.result = TimeService.today();
        };

        return api.process(request);
    }
}
